home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / msql-1.0.6 / src / common / strlib.c < prev    next >
C/C++ Source or Header  |  1994-09-07  |  4KB  |  238 lines

  1. /*
  2. **    strlib.c    - "Dynamic" versions of string routines
  3. **
  4. **
  5. ** Copyright (c) 1993  David J. Hughes
  6. **
  7. ** Permission to use, copy, and distribute for non-commercial purposes,
  8. ** is hereby granted without fee, providing that the above copyright
  9. ** notice appear in all copies and that both the copyright notice and this
  10. ** permission notice appear in supporting documentation.
  11. **
  12. ** This software is provided "as is" without any expressed or implied warranty.
  13. **
  14. */
  15.  
  16. #ifndef lint
  17. static char RCSid[] = 
  18.     "strlib.c,v 1.3 1994/08/19 08:02:59 bambi Exp";
  19. #endif 
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <varargs.h>
  24. #include <common/portability.h>
  25.  
  26.  
  27. #define    STR_LEN 4096
  28.  
  29. char    strBuf[STR_LEN];    /* 4K buffer used by all d* routines */
  30.  
  31.  
  32.  
  33. #ifndef HAVE_STRDUP
  34. char    *strdup(s)
  35.     char    *s;
  36. {
  37.     char    *s1;
  38.  
  39.     if (!s)
  40.         return(NULL);
  41.     s1 = (char *)malloc(strlen(s) + 1);
  42.     if (!s1)
  43.         return(NULL);
  44.     
  45.     strcpy(s1,s);
  46.     return(s1);
  47. }
  48. #endif
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. /****************************************************************************
  56. **     _dsprintf 
  57. **
  58. **    Purpose    : A sprintf that handles dynamic allocation (sort of)
  59. **    Args    : varargs (like printf)
  60. **    Returns    : char * to newly allocated string
  61. **    Notes    : This is to handle creation of queries that could be
  62. **          just about any length.  Allocating one large block
  63. **          here makes more sense (and stuffs up the heap less
  64. **          than using large local arrays in each proc call.
  65. */
  66.  
  67.  
  68. char *dsprintf(va_alist)
  69.     va_dcl
  70. {
  71.     va_list args;
  72.     int    len;
  73.     char    *fmt,
  74.         *tmp;
  75.  
  76.     va_start(args);
  77.     fmt = va_arg(args, char *);
  78.     (void) bzero(strBuf,STR_LEN);
  79.     (void) vsprintf(strBuf,fmt,args);
  80.     len = strlen(strBuf);
  81.     tmp = (char *)strdup(strBuf);
  82.     if (!tmp)
  83.     {
  84.         fprintf(stderr,"\ndsprintf() Out of memory\n\n");
  85.         exit(1);
  86.     }
  87.     return(tmp);
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. /****************************************************************************
  97. **     _dcsprintf
  98. **
  99. **    Purpose    : A combination dynamic sprintf and strcat
  100. **    Args    : Original string
  101. **        : printf styled vararg list
  102. **    Returns    : pointer to new string
  103. **    Notes    : old string is freed
  104. */
  105.  
  106. char *dcsprintf(va_alist)
  107.     va_dcl
  108. {
  109.     va_list    args;
  110.     char    *s1,
  111.         *tmp,
  112.         *fmt;
  113.  
  114.     va_start(args);
  115.     s1 = va_arg(args, char *);
  116.     fmt = va_arg(args, char *);
  117.     (void)bzero(strBuf,STR_LEN);
  118.     if (s1)
  119.     {
  120.         (void)strcpy(strBuf,s1);
  121.         (void)free(s1);
  122.     }
  123.     (void) vsprintf(strBuf + strlen(strBuf),fmt,args);
  124.     tmp = (char *) strdup(strBuf);
  125.     if (!tmp)
  126.     {
  127.         fprintf(stderr,"\ndcsprintf() Out of memory\n\n" );
  128.         exit(1);
  129.     }
  130.     return(tmp);
  131. }
  132.  
  133.  
  134. char *dstrcat(s1,s2)
  135.     char    *s1,
  136.         *s2;
  137. {
  138.     char    *tmp;
  139.  
  140.     (void)bzero(strBuf,STR_LEN);
  141.     if (s1)
  142.         strcpy(strBuf,s1);
  143.     strcat(strBuf,s2);
  144.     tmp = (char *)strdup(strBuf);
  145.     return(tmp);
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. /****************************************************************************
  154. **     _Mstrtok
  155. **
  156. **    Purpose    : An strtok(3) that doesn't match spans of tok-sep's
  157. **    Args    : 
  158. **    Returns    : 
  159. **    Notes    : This is required so that a string like "foo::" will
  160. **          return "" as the second token using Mstrtok(NULL,":");
  161. */
  162.  
  163.  
  164. char    *strtokBuf = NULL;
  165.  
  166. char *Mstrtok(str,sep)
  167.     char    *str,
  168.         *sep;
  169. {
  170.     static char    *cp,
  171.             eos;
  172.     char    *eot,
  173.         *tok;
  174.  
  175.     if (str)
  176.     {
  177.         if (strtokBuf)
  178.             (void)free(strtokBuf);
  179.         strtokBuf = (char *)strdup(str);
  180.         cp = strtokBuf;
  181.         eos=0;
  182.     }
  183.  
  184.     if (*cp == 0)
  185.     {
  186.         /*
  187.         ** Force an end of string condition
  188.         */
  189.         return(NULL);
  190.     }
  191.  
  192.     eot = (char *)strpbrk(cp,sep);
  193.     if (eot)
  194.     {
  195.         tok = cp;
  196.         cp = eot + 1;
  197.         *eot = 0;
  198.         return(tok);
  199.     }
  200.     else
  201.     {
  202.         if (eos)
  203.             return(NULL);
  204.         eos = 1;
  205.         return(cp);
  206.     }
  207. }
  208.  
  209.  
  210. char *Mmalloc(length)
  211.     int    length;
  212. {
  213.     char    *buf;
  214.  
  215.     buf = (char *)malloc(length);
  216.     if (!buf)
  217.     {
  218.         fprintf(stderr,"\nMmalloc : Out of memory!  Core dumped.\n\n");
  219.         abort();
  220.         exit(1);  /* just in case SIGIOT is caught */
  221.     }
  222.     (void)bzero(buf,length);
  223.     return(buf);
  224. }
  225.  
  226. char *Mfree(ptr)
  227.     char    *ptr;
  228. {
  229.     if (!ptr)
  230.     {
  231.         fprintf(stderr,"\nMfree : Free of NULL pointer!  Core dumped.\n\n");
  232.         abort();
  233.         exit(1);  /* just in case SIGIOT is caught */
  234.     }
  235.     (void)free(ptr);
  236.     return(NULL);
  237. }
  238.